package com.sgb.kjwl.view.widget;

import android.content.Context;
import android.view.View;
import android.widget.TextView;

import com.contrarywind.adapter.WheelAdapter;
import com.contrarywind.listener.OnItemSelectedListener;
import com.contrarywind.view.WheelView;
import com.sgb.kjwl.R;
import com.swgk.core.dialog.BasePopWindow;
import com.swgk.core.dialog.PopViewHolder;

import java.util.ArrayList;
import java.util.List;

/**
 * 二级滚动选择器
 */
public class TwoRangePopWindow extends BasePopWindow {
    private WheelView wheelView1;
    private WheelView wheelView2;
    private TextView tvTitle;
    private List<SourceEntity> item1List = new ArrayList<>();
    private List<SourceEntity> item2List = new ArrayList<>();
    private OnCallBack onCallBack;
    private int startId = -1;
    private int endId = -1;
    private String startName = "0";
    private String endName = "0";

    public void setTitle(String title) {
        if (tvTitle != null) {
            tvTitle.setText(title);
        }
    }

    @Override
    public void setBgView(View bgView) {
        super.setBgView(bgView);
    }

    public TwoRangePopWindow(Context c) {
        super(c, DIR_DOWN_UP);
    }

    @Override
    protected int popLayout() {
        return R.layout.pop_window_time_range2;
    }

    @Override
    protected void initView(PopViewHolder holder) {
        TextView okView = holder.getPop().findViewById(R.id.ok_view);
        TextView resetView = holder.getPop().findViewById(R.id.reset_view);
        resetView.setOnClickListener(view -> dissMiss());
        tvTitle = holder.getPop().findViewById(R.id.title);
        wheelView1 = holder.getPop().findViewById(R.id.wheelview1);
        wheelView1.setCyclic(false);
        wheelView2 = holder.getPop().findViewById(R.id.wheelview4);
        wheelView2.setCyclic(false);
        wheelView1.setAdapter(new SelectAdapter(item1List));
        wheelView1.setOnItemSelectedListener(new OnItemSelectedListener() {
            /**
             * @param index
             */
            @Override
            public void onItemSelected(int index) {
                startId = item1List.get(index).getId();
                startName = item1List.get(index).getName();
            }
        });

        wheelView2.setAdapter(new SelectAdapter(item2List));
        wheelView2.setOnItemSelectedListener(new OnItemSelectedListener() {
            @Override
            public void onItemSelected(int index) {
                endId = item2List.get(index).getId();
                endName = item2List.get(index).getName();
            }
        });

        okView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onCallBack != null) {
                    if (startId < 0) {
                        startId = item1List.get(wheelView1.getCurrentItem()).getId();
                        startName = item1List.get(wheelView1.getCurrentItem()).getName();
                    }
                    if (endId < 0) {
                        endId = item2List.get(wheelView2.getCurrentItem()).getId();
                        endName = item2List.get(wheelView2.getCurrentItem()).getName();
                    }
                    onCallBack.result(startId, endId, startName + "-" + endName);
                }
                dissMiss();
            }
        });
    }

    public void setData(List<SourceEntity> starData, List<SourceEntity> endData) {
        this.item1List = starData;
        this.item2List = endData;
        wheelView1.setAdapter(new SelectAdapter(item1List));
        wheelView2.setAdapter(new SelectAdapter(item2List));
    }

    public void setCurrentItem(int starCurrentItem, int endCurrentItem) {
        if (wheelView1.getItemsCount() >= starCurrentItem) {
            wheelView1.setCurrentItem(starCurrentItem);
        }
        if (wheelView2.getItemsCount() >= endCurrentItem) {
            wheelView2.setCurrentItem(endCurrentItem);
        }
    }

    public class SelectAdapter implements WheelAdapter {

        private List<SourceEntity> items;

        SelectAdapter(List<SourceEntity> list) {
            items = list;
        }

        @Override
        public int getItemsCount() {
            return items == null ? 0 : items.size();
        }

        @Override
        public Object getItem(int index) {
            if (index >= 0 && index < items.size()) {
                return items.get(index).getName();
            }
            return "";
        }

        @Override
        public int indexOf(Object o) {
            return items.indexOf(o);
        }
    }

    public interface OnCallBack {
        void result(int startTime, int endTime, String content);
    }

    public void setOnCallBack(OnCallBack onCallBack) {
        this.onCallBack = onCallBack;
    }


    public static class SourceEntity {

        private int id;
        private String name;

        public SourceEntity(String n, int h) {
            this.name = n;
            this.id = h;
        }

        public int getId() {
            return id;
        }

        public void setId(int id) {
            this.id = id;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

    }
}
